package com.sentiment.parser.news;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.sentiment.jsparser.PhantomjsCore;
import com.sentiment.nlpalgo.bean.Double2;
import com.sentiment.nlpalgo.bean.Keywords;
import com.sentiment.nlpalgo.method.AlgoHttp;
import com.sentiment.tools.Format;
import com.sentiment.webcollector.model.Page;

public class WangyiNews implements NewsParser{
	private static final int MINLENGTH = 5;
	private static final Logger LOG = LoggerFactory.getLogger(WangyiNews.class);
	private Page page;
	public Page getPage() {
		return page;
	}

	public void setPage(Page page) {
		this.page = page;
	}

	private Document doc;
	private AlgoHttp algo  = new AlgoHttp();
	
	public static final String REGEX = ".*\\.163\\..*";
	
	public WangyiNews(Page page){
		if(page != null){
			this.page = page;
			LOG.info("Phantomjs解析动态页面完成 : " + this.REGEX);
		}
		else
			LOG.error("page is empty !");
	}
	
	public String getTitle(){
		Element ele = doc.getElementById("h1title");
		return ele.text();
	}
	
	public Date getReleaseTime(){
		Element root = null;
		if(root == null)
			root = doc.getElementById("epContentLeft");
		if(root == null)
			root = doc.getElementsByClass("ep-content-main").first();
		if(root == null){
			Thread t = Thread.currentThread();
			t.interrupt();
		}
		
		Element ele = root.getElementsByClass("ep-time-soure").first();
		
		String src = ele.ownText().replaceAll("[^0-9]", "_");
		System.out.println(src);
		return Format.string2Date(src,6);
	}
	
	public List<String> getText(){
		Element ele = doc.getElementById("endText");
		List<String> li = new ArrayList<String>();
		Elements pElems = ele.getElementsByTag("p");
		Iterator<Element> it = pElems.iterator();
		while(it.hasNext()){
			String s = it.next().text();
			if(s.length() > MINLENGTH)
				li.add(s);
		}
		return li;
	}
	
	public String getPublisher(){
		Element ele = doc.getElementById("ne_article_source");
		
		return ele.text();
	}
	
	public int getComment(){
		int[] i = CommentAndPageView();
		return i[0];
	}
	
	private int[] CommentAndPageView(){
		int[] i = {0,0};
		int index = 0;
		Element ele = doc.getElementsByClass("tie-info").first();
		Elements aElems = ele.getElementsByTag("a");
		
		for(Iterator<Element> it = aElems.iterator(); index < 2 ; index++)
			if(it.hasNext())
				i[index] = Integer.parseInt(it.next().text().replaceAll("[^0-9]", ""));
		return i;
	}
	
	public int getPageView(){
		int[] i = CommentAndPageView();
		return i[1];
	}
	
	public List<Double2> getSentiment(List<String> text){
		ArrayList<Double2> list = algo.sentiment(text, AlgoHttp.SENTIMENT_NEWS);
		return list;
	}
	
	public Double2 getSentiment(String text){
		List<String> list = new ArrayList<String>();
		list.add(text);
		ArrayList<Double2> result = algo.sentiment(list, AlgoHttp.SENTIMENT_NEWS);
		return result.get(0);
	}
	
	public static void main(String[] args) throws IOException{
//		Page page = new Page();
//		StringBuilder sb = new StringBuilder();
//		BufferedReader br = new BufferedReader(new FileReader("C:/Users/王骏科/Desktop/new2.html"));
//		String temp = null;
//		while((temp = br.readLine())!=null){
//			sb.append(temp);
//		}
//		page.setDoc(Jsoup.parse(sb.toString()));
//		WangyiNews n = new WangyiNews(page);
////		System.out.println(sb.toString());
//		System.out.println(n.getTitle());
//		Date d = n.getReleaseTime();
//		System.out.println(Format.date2String(d));
//		sb.delete(0, sb.length());
//		List<String> li = n.getText();
//		for(String s : li){
//			System.out.println(s);
//			sb.append(s);
//		}
//		System.out.println(sb);
//		System.out.println(n.getPublisher());
//		System.out.println(n.getComment());
//		System.out.println(n.getPageView());
		
//		System.out.println(n.getSentiment(sb.toString()));
		
		
		Pattern p = Pattern.compile(WangyiNews.REGEX);
		Matcher m = p.matcher(new String("http://money163.com/api/15/1019/10/B69I4SGB00252G50.html"));
		System.out.println(m.find());
		
//		DbUtil du = new DbUtil();
//		du.openConnection(CrawlInfo.dbName, CrawlInfo.collection);
//		CrawlInfo ci = new CrawlInfo(new Date(), "", "news", n.getTitle(), n.getText(), "", n.getPublisher(), n.getReleaseTime(), 0, n.getPageView(),n.getComment(), n.getSentiment(sb.toString()).getData1());
//		du.insert(ci.toDocument());
//		du.closeConnection();
	}

	@Override
	public List<Keywords> getKeywords(List<String> text) {
		// TODO Auto-generated method stub
		List<Keywords> li =  algo.keywords(text);
		return li;
	}

	@Override
	public Keywords getKeywords(String text) {
		// TODO Auto-generated method stub
		List<String> li = new ArrayList<String>();
		li.add(text);
		List<Keywords> result = algo.keywords(li);
		return result.get(0);
	}
}
